import java.util.Iterator;
import java.util.NoSuchElementException;

public class graphcs {
    public static void main(String[] args)
    {
        graph a=new graph(9);
        a.addEdge(0, 2);
        a.addEdge(2, 1);
        a.addEdge(0, 1);
        a.addEdge(3, 8);
        a.addEdge(5, 2);
        a.addEdge(7, 6);
        System.out.println(a);
    }
}

class graph
{
    public final int V;
    public int E;
    private Bag<Integer>[]adj;
    public graph(int V)
    {
        this.V=V;this.E=0;
        adj=(Bag<Integer>[])new Bag[V];
        for(int i=0;i<V;i++)
            adj[i]=new Bag<Integer>();
    }
    public void addEdge(int v,int w)
    {
        adj[v].add(w);
        adj[w].add(v);
        E++;
    }
    public Iterable<Integer> adj(int v)
    {
        return adj[v];
    }
    public String toString()
    {
        String s="顶："+V+" 边："+E+"\n";
        for(int i=0;i<V;i++)
        {
            s+=i+": ";
            for(int w:this.adj(i))
                s+=w+" ";
            s+="\n";
        }
        return s;

    }
}

class Bag<Item> implements Iterable<Item> {
    private Node<Item> first;    // beginning of bag
    private int n;               // number of elements in bag

    // helper linked list class
    private static class Node<Item> {
        private Item item;
        private Node<Item> next;
    }

    /**
     * Initializes an empty bag.
     */
    public Bag() {
        first = null;
        n = 0;
    }

    /**
     * Returns true if this bag is empty.
     *
     * @return {@code true} if this bag is empty;
     *         {@code false} otherwise
     */
    public boolean isEmpty() {
        return first == null;
    }

    /**
     * Returns the number of items in this bag.
     *
     * @return the number of items in this bag
     */
    public int size() {
        return n;
    }

    /**
     * Adds the item to this bag.
     *
     * @param  item the item to add to this bag
     */
    public void add(Item item) {
        Node<Item> oldfirst = first;
        first = new Node<Item>();
        first.item = item;
        first.next = oldfirst;
        n++;
    }


    /**
     * Returns an iterator that iterates over the items in this bag in arbitrary order.
     *
     * @return an iterator that iterates over the items in this bag in arbitrary order
     */
    public Iterator<Item> iterator()  {
        return new LinkedIterator(first);  
    }

    // an iterator, doesn't implement remove() since it's optional
    private class LinkedIterator implements Iterator<Item> {
        private Node<Item> current;

        public LinkedIterator(Node<Item> first) {
            current = first;
        }

        public boolean hasNext()  { return current != null;                     }
        public void remove()      { throw new UnsupportedOperationException();  }

        public Item next() {
            if (!hasNext()) throw new NoSuchElementException();
            Item item = current.item;
            current = current.next; 
            return item;
        }
    }



}
